import * as types from './constans';
import { reqTopList, reqTopListDetail } from '@/api/toplist';
const changeTopList = (list) => ({ type: types.CHANGE_TOP_LIST, list })

const changeSongsList = (list) => ({
    type: types.CHANGE_SONGS_LIST,
    list
})

const changeTitleInfo = (info) => ({ type: types.CHANGE_TITLE_INFO, info })

export const getTopList = () => {
    return dispatch => {
        reqTopList().then(res => {
            if (res.code === 200) {
                dispatch(changeTopList(res.list))
            }
        })
    }
}

export const getTopListDetail = id => {
    return dispatch => {
        reqTopListDetail(id || types.DETAIL_NUM).then(res => {
            if (res.code !== 200) {
                return
            }
            const {
                coverImgUrl,
                name,
                trackNumberUpdateTime,
                playCount,
                subscribedCount,
                commentCount,
                shareCount,
                tracks
            } = res.playlist
            const titleInfo = {
                coverImgUrl,
                name,
                trackNumberUpdateTime,
                playCount,
                subscribedCount,
                commentCount,
                shareCount,
            }
            dispatch(changeSongsList(tracks))
            dispatch(changeTitleInfo(titleInfo))

        })
    }
}